package com.bidevalution.controller.api;

import com.bidevalution.business.service.BeExpertRecommendCandidateService;
import com.bidevalution.dao.entity.BeExpertRecommendCandidate;
import com.qtp.common.core.TSUser;
import com.qtp.common.data.RedisAccess;
import com.qtp.core.model.Response;
import com.qtp.core.util.ParamsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 标段报表控制层
 *
 * @author 查慧英
 * @date 2019-11-20 17:45
 */
@RestController
@RequestMapping("/beRecommendCandidate")
@Api(description = "推荐候选单位")
public class BeExpertRecommendCandidateController extends BaseController {
    private Logger logger = Logger.getLogger(getClass());
    private static final String SECTION_ID = "sectionId";
    private static final String SEQ_IDS = "seqIds";
    private static final String USER_GUID = "userGuid";
    private static final String ENTERPRISE_IDS = "enterpriseIds";
    private static final String RANKS = "ranks";
    private static final String EXPERT_ID = "expertId";
    private static final String LOGIN_NAME = "loginName";
    @Autowired(required = false)
    BeExpertRecommendCandidateService recommendCandidateService;
    /**
     * 获取中标候选人列表
     *  @param request
     * @param response
     */
    @ApiOperation(value = "获取中标候选人列表", notes = "获取中标候选人列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = SECTION_ID, value = "标段Id", required = true, paramType = "query", dataType = "String")
    })
    @PostMapping("/getRecommendCandidateList")
    public Response<List<BeExpertRecommendCandidate>> getRecommendCandidateList(HttpServletRequest request, HttpServletResponse response) {
        setHttpServletResponse(response);
        try {
            // 获取请求中的参数
            String sectionId = request.getParameter(SECTION_ID);
            String userGuid = request.getHeader(USER_GUID);
            TSUser tsUser = RedisAccess.getValue(TSUser.class, userGuid);
            if (tsUser != null) {
                Map<String, Object> params = new HashMap<>(4);
                params.put(SECTION_ID, sectionId);
                params.put(LOGIN_NAME, tsUser.getLoginName());
                return recommendCandidateService.getRecommendCandidateList(params);
            } else {
                return Response.fail("获取专家账号登录信息失败");
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return null;
    }
    /**
     * 新增修改企业排名
     *  @param request
     * @param response
     */
    @ApiOperation(value = "新增修改企业排名", notes = "新增修改企业排名")
    @ApiImplicitParams({
            @ApiImplicitParam(name = SECTION_ID, value = "标段Id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = SEQ_IDS, value = "所有主键，用逗号分隔", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = EXPERT_ID, value = "专家id", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = ENTERPRISE_IDS, value = "所有企业ID，用逗号分隔", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = RANKS, value = "所有企业排名，用逗号分隔", paramType = "query", dataType = "Integer")
    })
    @PostMapping("/addOrUpdate")
    public Response addOrUpdate(HttpServletRequest request, HttpServletResponse response) {
        setHttpServletResponse(response);
        try {
            // 获取请求中的参数
            Map<String, Object> params = ParamsUtil.getParamsValue(request);
            return recommendCandidateService.addOrUpdate(params);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return null;
    }
}
